package sqlite;

import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by admin on 2017/7/1.
 */
public class MySQLiteOpenHelper extends SQLiteOpenHelper {

    private volatile static MySQLiteOpenHelper sqLiteOpenHelper;
    private static SQLiteDatabase database;


    public static MySQLiteOpenHelper getInstance(Context mContext) {
        if (sqLiteOpenHelper == null) {
            synchronized (MySQLiteOpenHelper.class) {
                if (sqLiteOpenHelper == null) {
                    sqLiteOpenHelper = new MySQLiteOpenHelper(mContext, "my.db", null, 1);
                }
            }
        }
        return sqLiteOpenHelper;
    }

    public static SQLiteDatabase getDatabase(Context mContext) {
        if (database == null) {
            if (sqLiteOpenHelper == null) {
                sqLiteOpenHelper = new MySQLiteOpenHelper(mContext, "my.db", null, 1);
            }
            database = sqLiteOpenHelper.getReadableDatabase();
        }
        return database;
    }

    public static void closeSQLite() {
        if (sqLiteOpenHelper != null) {
            sqLiteOpenHelper.close();
        }
        if (database != null) {
            database.close();
        }
    }


    public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) {
        super(context, name, factory, version, errorHandler);
    }

    public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //id integer primary key,epc varchar, name varchar,ishave varchar
        db.execSQL("create table if not exists goodsInfo(id integer primary key,epc varchar)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
